# coding=utf8
from __future__ import unicode_literals
'''


@author: Soul
'''

from lxml import *
import lxml.html
import urllib2
from lxml.html import fromstring

def getjarinfo(url):
    proxy_needed = False
    if proxy_needed:
        proxy_handler = urllib2.ProxyHandler({'http' : 'http://cn-proxy.jp.oracle.com:80'})
        opener = urllib2.build_opener(proxy_handler, urllib2.HTTPHandler)
        urllib2.install_opener(opener)
    
    c = None
    try:
        headers = {'User-Agent':'Mozilla/5.0 (Windows; U; Windows NT 6.1; en-US; rv:1.9.1.6) Gecko/20091201 Firefox/3.5.6'}
        req = urllib2.Request(url=url, headers=headers)
        c = urllib2.urlopen(req)
    except Exception as e:
        print e
        
    if not c == None:
        f = c.read()
        pasred_html = fromstring(f)
        #
        # Replace the xpath here to validate your xpath value
        #
        x_content = pasred_html.xpath("//table[@id='resultList']/tr")
        if isinstance(x_content, list):
            print len(x_content)
            if len(x_content) > 0:
                x_content = x_content[0]
            else:
                x_content = ''
        
        value = ''
        if hasattr(x_content, 'text_content'):
            value = x_content.text_content()
        else:
            value = x_content                                                
        
        removelist = [u'\\xa0', u'\\r', u'\\n', u' ', u'\\t', u'\\a', u'\\b', u'\\f', u'\\v', u'\\0']
        import string
        if isinstance(value, unicode):
            t = value.encode('unicode_escape')
            for rl in removelist:
                t = t.replace(rl, u'')
            
            start = 0
            count = 0
            while start < len(t):
                i = t.find('\\', start, len(t))
                if t[i + 1] == ('x') or t[i + 1] == ('X'):
                    count = count + 1
                if start > i:
                    break
                start = i + 1
            
            if t.count('\\') == 0:
                rate = 0
            else:
                rate = (float)(count) / (float)(t.count('\\'))
            if rate > 0.5:
                value = value.encode('raw_unicode_escape')
        print unicode(value)
    
if __name__ == '__main__':
    #
    # Replace the url to the page URL you want to test
    #
    url = 'http://search.51job.com/jobsearch/search_result.php?fromJs=0&keyword=%E7%94%B5%E5%8A%A8%E6%B1%BD%E8%BD%A6&curr_page=2&ord_field=1'
    getjarinfo(url)
